package com.freecloud.javabasics.design.single;

import com.freecloud.javabasics.design.entity.car.Car;
import com.freecloud.javabasics.design.entity.car.DaZhong;

/**
 * 单例：懒汉模式，在调用时创建
 * @Author: maomao
 * @Date: 2019-10-30 21:52
 */
public class Lazy {

    private static Car car = null;

    /**
     * 普通单例模式，有线程安全问题
     * @return
     */
    public static Car getInstance(){
        if(car != null){
            return car;
        }
        car = new DaZhong();
        return car;
    }

    /**
     * 在方法上增加synchronized关键字实现单利安全（性能较差）
     * @return
     */
    public static synchronized Car getInstanceSync(){
        return getInstance();
    }

}
